//
//  MainRecommendViewController.m
//  Funny
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 King. All rights reserved.
//

#import "MainRecommendViewController.h"

@interface MainRecommendViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MainRecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self.view addSubview:self.tableView];
    self.title = @"热门景点";
    // Do any additional setup after loading the view.
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight + 22) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellAccessoryNone;
        _tableView.showsVerticalScrollIndicator = NO;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [_tableView registerClass:[MianRecommendTableViewCell class] forCellReuseIdentifier:@"reuse"];
    }
    return _tableView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenWidth * 3 / 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MianRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.model = self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    MainAnuseViewController *mianVC = [MainAnuseViewController shareViewController];
    mianVC.ID = [_dataArr[indexPath.row] ID];
    [self.navigationController pushViewController:mianVC animated:NO];
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return  _dataArr;
}

// 数据处理
- (void)handleData
{
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/area/getIndexData?&cityid=%@",self.ID];
    [NetHandler getDataWithURL:url completion:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"geo"];
        for (NSDictionary *dataDic in arr) {
            MainRecommendModel *model = [MainRecommendModel modelWithDic:dataDic];
            [self.dataArr addObject:model];
            [self.tableView reloadData];
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
